package ework;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;

public class ScreenCapture extends Frame {
    private static final long serialVersionUID = 1L;
    private static int startX;
    private static int startY;
    private static int endX;
    private static int endY;

    private static int x = 0;
    private static int y = 0;
    private static int width = 0;
    private static int height = 0;

    private static Robot robot;
    private static String path;

    private static int count = 0;

    public static void run(Long time, String filename) {
        ScreenCapture jt = new ScreenCapture();
        Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
        jt.setSize(screenSize);
        jt.setUndecorated(true);
        jt.setOpacity(0.5f);
        jt.setVisible(true);
        jt.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
                    jt.dispose();
                } else if (e.getKeyCode() == KeyEvent.VK_BACK_SPACE) {
                    jt.dispose();
                } else if (e.getKeyCode() == KeyEvent.VK_ENTER) {
                    JFileChooser fc = new JFileChooser();
                    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                    fc.setDialogTitle("Select the storage path");
                    fc.setApproveButtonText("OK");
                    if (fc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
                        jt.setOpacity(0.01f);
                        path = fc.getSelectedFile().getAbsolutePath();
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e1) {
                            e1.printStackTrace();
                        }
                        jt.dispose();
                        while (true) {
                            try {
                                captureScreen(new Rectangle(x, y, width, height), path, filename);
                            } catch (Exception exception) {
                                exception.printStackTrace();
                            }
                            try {
                                Thread.sleep(time);
                            } catch (InterruptedException interruptedException) {
                                interruptedException.printStackTrace();
                            }
                        }
                    }
                }
            }
        });
        jt.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                Point point = MouseInfo.getPointerInfo().getLocation();
                startX = (int) point.getX();
                startY = (int) point.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {

            }
        });
        jt.addMouseMotionListener(new MouseAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {
                Point point = MouseInfo.getPointerInfo().getLocation();
                endX = (int) point.getX();
                endY = (int) point.getY();

                Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize();
                Graphics2D g = (Graphics2D) jt.getGraphics();
                g.clearRect(0, 0, (int) dimension.getWidth(), (int) dimension.getHeight());
                g.setColor(Color.RED);
                g.setStroke(new BasicStroke(2.0f));

                if (endX - startX > 0 && endY - startY > 0) {
                    x = startX;
                    y = startY;
                    width = endX - startX;
                    height = endY - startY;
                } else if (endX - startX < 0 && endY - startY < 0) {
                    x = endX;
                    y = endY;
                    width = startX - endX;
                    height = startY - endY;
                } else if (endX - startX > 0 && endY - startY < 0) {
                    x = startX;
                    y = endY;
                    width = endX - startX;
                    height = startY - endY;
                } else if (endX - startX < 0 && endY - startY > 0) {
                    x = endX;
                    y = startY;
                    width = startX - endX;
                    height = endY - startY;
                }
                g.drawRect(x, y, width, height);
            }
        });
    }

    private static void captureScreen(Rectangle rect, String folder, String filename) throws Exception {
        if (robot == null) {
            robot = new Robot();
        }
        BufferedImage image = robot.createScreenCapture(rect);
        File screenFile = new File(folder);
        if (!screenFile.exists()) {
            screenFile.mkdir();
        }
        File file;
        count++;
        if (filename == null) {
            file = new File(screenFile, System.currentTimeMillis() + ".png");
        } else {
            file = new File(screenFile, filename + count + ".png");
        }
        ImageIO.write(image, "png", file);
        System.out.println("已截图" + count + "张");
    }
}
